package com.github.baichuan.application.dashboard.feign;

import com.github.baichuan.application.dashboard.dto.auth.ChangePassword;
import com.github.baichuan.application.dashboard.dto.auth.PasswordCredential;
import com.github.baichuan.web.dto.Result;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * 身份鉴定和授权
 */
@Component
@Slf4j
public class AuthService {
    private Environment env;
    private WebClient webClient;

    @Autowired
    public AuthService(Environment env){
        this.env = env;
    }

    @PostConstruct
    public void init(){
        webClient = WebClient.builder()
                .baseUrl(env.getRequiredProperty("service.application-skeleton.url"))
                .defaultHeader("x-application-code", env.getRequiredProperty("service.application-skeleton.code"))
                .build();
    }

    /**
     * 用户登入验证
     * @param credential
     * @return
     */
    public Mono<Result> signin(PasswordCredential credential){
        return webClient.post().uri("/auth/password")
                .bodyValue(credential)
                .retrieve()
                .bodyToMono(Result.class);
    }

    public Mono<Result> changePassword(ChangePassword changePassword){
        return webClient.post().uri("/application/member/password/change")
                .bodyValue(changePassword)
                .retrieve()
                .bodyToMono(Result.class);
    }
}
